//===================================================================================
// Microsoft patterns & practices
// Silk : Web Client Guidance
//===================================================================================
// Copyright (c) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===================================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===================================================================================
using System.Collections.Generic;
using System.Data;
using System.Linq;
using DataTier.DataAccess.UnitOfWork;
using DataTier.DataCore;
using DataTier.DataCore.Repositories;
using DataTier.DataCore.UnitOfWork;
using DataTier.Model;
using DataTier.Model.Entities;

namespace DataTier.DataAccess.Repositories
{
    public class VehicleRepository : Repository<Vehicle, int>, IVehicleRepository
    {
        public VehicleRepository(IQueryableUnitOfWork unitOfWork)
            : base(unitOfWork)
        {
        }

        public void Create(int userId, Vehicle vehicle)
        {
            var context = this.UnitOfWork as MainModuleUnitOfWork;
            if (context != null)
            {
                vehicle.UserId = userId;
                context.CreateSet<Vehicle>().Add(vehicle);
                this.UnitOfWork.Commit();
            }
        }

        public Vehicle GetVehicle(int userId, int vehicleId)
        {
            var context = this.UnitOfWork as MainModuleUnitOfWork;
            if (context != null)
            {
                return context.CreateSet<Vehicle>()
                           .Include("Fillups")
                           .Include("Reminders")
                           .Single(v => v.VehicleId == vehicleId && v.UserId == userId);
            }
            return null;
        }

        public IEnumerable<Vehicle> GetVehicles(int userId)
        {
            var context = this.UnitOfWork as MainModuleUnitOfWork;
            if (context != null)
            {
                return context.CreateSet<Vehicle>()
                           .Include("Fillups")
                           .Include("Reminders")
                           .Where(v => v.UserId == userId);
            }
            return null;
        }

        public void Update(Vehicle updatedVehicle)
        {
            var context = this.UnitOfWork as MainModuleUnitOfWork;
            if (context != null)
            {
                Vehicle vehicleToUpdate =
                    context.CreateSet<Vehicle>().First(v => v.VehicleId == updatedVehicle.VehicleId);

                vehicleToUpdate.Name = updatedVehicle.Name;
                vehicleToUpdate.Year = updatedVehicle.Year;
                vehicleToUpdate.MakeName = updatedVehicle.MakeName;
                vehicleToUpdate.ModelName = updatedVehicle.ModelName;
                vehicleToUpdate.SortOrder = updatedVehicle.SortOrder;
                vehicleToUpdate.PhotoId = updatedVehicle.PhotoId;

                context.SetEntityState(vehicleToUpdate, vehicleToUpdate.VehicleId == 0
                                                            ? EntityState.Added
                                                            : EntityState.Modified);
                this.UnitOfWork.Commit();
            }
        }

        public void Delete(int vehicleId)
        {
            var context = this.UnitOfWork as MainModuleUnitOfWork;
            if (context != null)
            {
                Vehicle vehicle =
                    context.CreateSet<Vehicle>().Single(v => v.VehicleId == vehicleId);

                context.CreateSet<Vehicle>().Remove(vehicle);

                this.UnitOfWork.Commit();
            }
        }

        public override Vehicle Get(int id)
        {
            var context = this.UnitOfWork as MainModuleUnitOfWork;
            if (context != null)
            {
                return context.CreateSet<Vehicle>().Find(id);
            }
            return null;
        }
    }
}
